package com.ztgf.order.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.order.models.bo.OrderRightsProtectionBO;
import com.ztgf.order.models.bo.OrderRightsProtectionSaveBO;
import com.ztgf.order.models.dto.OrderRightsProtectionDTO;
import com.ztgf.order.models.service.IOrderRightsProtectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 商家维权表控制器
 *
 * @author YUE
 * @Date 2020-06-29 15:16:02
 */

@RestController
@RequestMapping("/cms/orderRightsProtection")
@Slf4j
@Api(value = "OrderRightsProtectionController",tags = "商家维权表接口")
public class OrderRightsProtectionController {

    @DubboReference
    private IOrderRightsProtectionService orderRightsProtectionService;

    /**
     * 审核维权
     * @param orderRightsProtectionBO
     * @return
     */
    @PostMapping(value = "/rightsProtectionAudit")
    @ApiOperation(value = "维权", notes = "维权")
    public Result rightsProtectionAudit(OrderRightsProtectionBO orderRightsProtectionBO){
        if(null == orderRightsProtectionBO.getOrderId()){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean b = orderRightsProtectionService.updateResultById(orderRightsProtectionBO);
        if(b){
            return new Result();
        }
        return new Result<>(CodeEnum.FAIL_100001);
    }



    @ApiOperation(value = "添加商家维权表",notes = "添加商家维权表")
    @PostMapping(value = "/insert")
    public Result insert(OrderRightsProtectionSaveBO saveBO) {
        boolean flag = orderRightsProtectionService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存商家维权表",notes = "保存商家维权表")
    public Result save(OrderRightsProtectionSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改商家维权表",notes = "修改商家维权表")
    @PostMapping("/update")
    public Result update(OrderRightsProtectionSaveBO saveBO){
        OrderRightsProtectionDTO dto = new OrderRightsProtectionDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = orderRightsProtectionService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除商家维权表",notes = "删除商家维权表")
    @ApiImplicitParam(name = "id",value = "商家维权表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = orderRightsProtectionService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除商家维权表",notes = "批量删除商家维权表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        orderRightsProtectionService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<OrderRightsProtectionDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        OrderRightsProtectionDTO dto = orderRightsProtectionService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "商家维权表列表",notes = "商家维权表列表")
    @PostMapping("/queryPageList")
    public Result<Page<OrderRightsProtectionDTO>> queryPageList(OrderRightsProtectionBO bo){
        Wrapper<OrderRightsProtectionDTO> wrapper = new EntityWrapper<>();
        if (StrUtils.isNotEmpty(bo.getStartDate())){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (StrUtils.isNotEmpty(bo.getEndDate())){
            wrapper.le("create_time", bo.getEndDate());
        }
        if(null != bo.getOrderId()){
            wrapper.eq("order_id", bo.getOrderId());
        }
        if(StrUtils.isNotEmpty(bo.getBuyerName())){
            wrapper.like("buyer_name", bo.getBuyerName());
        }
        if(StrUtils.isNotEmpty(bo.getSellerName())){
            wrapper.like("seller_name", bo.getSellerName());
        }
        if(StrUtils.isNotEmpty(bo.getOrderNo())){
            wrapper.eq("order_no", bo.getOrderNo());
        }
        wrapper.orderBy("create_time", false); //默认倒序
        Integer result = bo.getResult();
        if(null != result){
            if(result == 3){
                wrapper.eq("result",0);
            }
            else
            {
                wrapper.in("result",Arrays.asList(1,2));
            }
        }
        Page<OrderRightsProtectionDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = orderRightsProtectionService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
